import path from 'path';

import { runHvigorAsync } from '../../common/common';
import { addOhPackageDps, commonFileOperation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';
import { getJson5Obj } from '../../../../src/utils/json-util';
import fse from 'fs-extra';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.FA,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.LiteEmptyAbility,
  },
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();
  const packageJsonPath = path.resolve(projectRoot, 'oh-package.json5');
  const packageJson = getJson5Obj(packageJsonPath);
  packageJson.devDependencies['@ohos/hypium'] = '1.0.19';
  fse.writeFileSync(packageJsonPath, JSON.stringify(packageJson));

  const commands: string[] = '--sync -p product=default --analyze=normal --parallel --incremental --daemon'.split(' ');
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('构建liteWearable模板工程，支持引用hypium', result);
  expect(result.stdout).toContain('Finished :entry:init');
  expect(result.stdout).toContain('Finished ::init');
};

TestCaseBuilder.builder()
  .caseName('DTS2024091916669')
  .description('构建liteWearable模板工程，支持引用hypium')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();
